home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / diskutil / partcopy.lzh / PARTCOPY.C < prev    next >
C/C++ Source or Header  |  1988-12-29  |  5KB  |  185 lines

  1. /*
  2.  * Hard Disk partition copier
  3.  *
  4.  * by Dave Small and Dan Moore
  5.  * Copyright 1988 Antic Publishing, Inc.
  6.  *
  7.  * copy one HD partition to another.
  8.  *
  9.  * BE VERY CAREFUL WITH THIS!!!!!
  10.  *
  11.  * dlm --- 09/14/87
  12.  *
  13.  * dlm --- 11/22/88    added a useable front end for START
  14.  *
  15.  */
  16. #include <portab.h>
  17. #include <osbind.h>
  18. #include <stdio.h>
  19.  
  20. #define DEBUG        0        /* print numbers, no i/o */
  21. #define REAL        1        /* copy code enabled? */
  22.  
  23. /*
  24.  * More external routines.  These are the DMA buss i/o routines
  25.  * that Supra uploaded to the Developers Forum on Compuserve (PCS 57)
  26.  *
  27.  * Dave and I would like to thank Supra for releasing the source
  28.  * code to these routines.
  29.  *
  30.  */
  31. extern int nhd_read();
  32. extern int nhd_write();
  33.  
  34. #define BUF_SECS (127)
  35. #define BUF_SIZE (512L * BUF_SECS)
  36.  
  37.  /* the backup buffers and variables */ 
  38. long    hd_buff;        /* our disk i/o buffer            */
  39.  
  40. /*
  41.  * globals
  42.  */
  43. int    src_device, dst_device;        /* SCSI device to use        */
  44. int    src_unit, dst_unit;        /* logical unit on device    */
  45. long    src_sector, dst_sector;
  46. long    copy_len;
  47.  
  48. /*
  49.  * the actual, magical hard disk partition copier.
  50.  * the program no one can live without.
  51.  */
  52. int
  53. do_copy()
  54. {
  55.     register unsigned int  sec_count, stat;
  56.     register unsigned long curr_sec;
  57.     register unsigned long total_sectors;
  58.     int error = 0, pass = 0;
  59.  
  60.     total_sectors = copy_len - 1;    /* how many to dup */
  61.     sec_count     = BUF_SECS;
  62.  
  63.     /* now do the copy.  */
  64.     for (curr_sec = 0; curr_sec < total_sectors && sec_count; curr_sec += sec_count, pass++) {
  65.  
  66.         /* adjust number of sectors if nearing the end of the copy */
  67.         if (curr_sec + sec_count >= copy_len)
  68.             sec_count = copy_len - curr_sec - 1;
  69.  
  70.         if (pass % 8 == 0) {
  71.             printf("Now reading sector %ld\n", curr_sec + src_sector);
  72.         }
  73.  
  74.         /* nhd_read(sectno.L,count.W,buff.L,dma.W) */
  75. #if DEBUG
  76.         printf("read sector: %ld to %ld \n", curr_sec + src_sector, curr_sec + sec_count + src_sector);
  77.         stat = 0;
  78. #else
  79.         stat = nhd_read(curr_sec + src_sector, sec_count, hd_buff, src_device, src_unit);
  80. #endif
  81.         if (stat) {
  82.             printf("READ ERROR! At sector %ld.\n", curr_sec + src_sector);
  83.             error = 1;
  84.             break;
  85.         }
  86.  
  87. #if DEBUG
  88.         printf("\twrite sector: %ld to %ld\n", curr_sec + dst_sector, curr_sec + sec_count + dst_sector);
  89.         stat = 0;
  90. #else
  91.         stat = nhd_write(curr_sec + dst_sector, sec_count, hd_buff, dst_device, dst_unit);
  92. #endif
  93.         if (stat) {
  94.             printf("WRITE ERROR! At sector %ld.\n", curr_sec + dst_sector);
  95.             error = 1;
  96.             break;
  97.         }
  98.     }
  99.     return (error);
  100. }
  101.  
  102. void
  103. get_long(prompt, variable, min, max)
  104. char *prompt;
  105. long *variable;
  106. long  min, max;
  107. {
  108.     char   input[80];
  109.     register long temp;
  110.     extern long atol();
  111.  
  112.     do {
  113.         printf(prompt);
  114.         gets(input);
  115.         temp = atol(input);
  116.     } while (temp > max || temp < min);
  117.  
  118.     *variable = temp;
  119. }
  120.  
  121. void
  122. get_int(prompt, variable, min, max)
  123. char *prompt;
  124. int  *variable;
  125. int  min, max;
  126. {
  127.     long temp;
  128.     get_long(prompt, &temp, (long) min, (long) max);
  129.     *variable = (int)temp;
  130. }
  131.  
  132. main()
  133. {
  134.     char input[80];
  135.  
  136.     setbuf(stdout, NULL);    /* unbuffered output */
  137.     setbuf(stdin, NULL);    /* unbuffered input */
  138.  
  139.     puts("\t\tHard Disk Partition Copier");
  140.     puts("\t\tby Dan Moore and Dave Small");
  141.     puts("\t\tCopyright (c) 1988 by Antic Publishing\n\n");
  142.  
  143.     if ((hd_buff = Malloc(BUF_SIZE)) == NULL) {
  144.         puts("Unable to allocate work buffer\nRETURN to exit.");
  145.         getchar();
  146.     }    
  147.  
  148.     do {
  149.  
  150.         get_int("Enter source SCSI device: ", &src_device, 0, 7);
  151.         get_int("Enter source SCSI unit: ", &src_unit, 0, 1);
  152.         get_long("Enter starting sector: ", &src_sector, 0L, 0x7fffffffL);
  153.         get_int("Enter destination SCSI device: ", &dst_device, 0, 7);
  154.         get_int("Enter destination SCSI unit: ", &dst_unit, 0, 1);
  155.         get_long("Enter destination sector: ", &dst_sector, 0L, 0x7fffffffL);
  156.         get_long("Enter length: ", ©_len, 1L, 0x7fffffffL);
  157.  
  158.         printf("\n\nSOURCE: device %d\tlun %d\tsector %ld\n", src_device, src_unit, src_sector);
  159.         printf("DEST:   device %d\tlun %d\tsector %ld\n", dst_device, dst_unit, dst_sector);
  160.         printf("copy length %ld\n\n", copy_len);
  161.  
  162.         printf("If you have ANY doubts DO NOT CONTINUE!!\n");
  163.         printf("An incorrect copy can DESTROY the data on your drive!!\n\n");
  164.         printf("Type \"yes\" to copy sectors: ");
  165.         gets(input);
  166.  
  167.         if (strcmp(input, "yes") == 0) {
  168. #if REAL
  169.             if (!do_copy())    /* do it toit */
  170.                 puts("It won.\n");
  171.             else
  172.                 puts("It lost.\n");
  173. #else
  174.             puts("copy code goes here!");
  175. #endif
  176.         }
  177.  
  178.         printf("\n\nType \"quit\" to exit: ");
  179.         gets(input);
  180.  
  181.     } while(strcmp(input, "quit"));
  182.  
  183.     Mfree(hd_buff);
  184. }
  185.